// pages/mine/mine.js

import request from "../../utils/request.js";
import auth from "../../utils/auth.js";
import local from "../../utils/local";
import nav from "../../utils/nav.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appid: 'wx15a7538fa77ddfc0',
    userLogo: "",
    companyName: "",
    userPhone: "生成会员",
    memberName: '',
    serviceUrl: '',
    memberID: '',
    isLogin: false,
    openId: '',
    memberLevel: '',
    overDate: '',
    PRICE_INFO: {},
    csDay: '',
    sex: '',
    show: false,
    selectedPlan: null,
    oneArr: [],
    twoArr: [],
    threeArr: [],
    fourArr: [],

    genCont: '',
    isAcc: '', // 0 未付款 1 已付款
    isAccGenCont: '',
    paiPanItemsCode: '',
    amo: '',
    juanCode: '',
    juanAmo: '',
    msAmo: '',
    settID: '',
    payAmo: '',
    transID: '',
    jndiName: '',
    nonceStr: '',
    notify_url: '',
    openId: '',
    package: '',
    prepay_id: '',
    sign: '',
    timeStamp: '',
    total_fee: '',
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // const tempMemberID = local.getLocal('memberID');
    // if (tempMemberID == '' || tempMemberID == '0') {
    //   nav.navigateTo('/login/login')
    //   return
    // }
  },

  handleToDetail() {
    console.log(this.data.memberID)
    if (this.data.memberID == '' || this.data.memberID == '0') {
      nav.navigateTo('/login/login')
    }else {
      nav.navigateTo('/mine/updateInfo/update-info')
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("11111");
    const tempMemberID = local.getLocal('memberID');
    const tempOpenId = local.getLocal('openId');
    // const tel = local.getLocal("tel");
    const memberName = local.getLocal("memberName");

    if (tempMemberID == '' || tempMemberID == '0'){
      this.setData({
        isLogin: false,
      })
    }else {
      this.setData({
        isLogin: true,
      })
      this.setData({
        memberID: tempMemberID,
        openId: tempOpenId,
        
      })
      this.getPaipanmemberservice();
    }
  },

  getPaipanmemberservice() {
    const that = this;
    let dic = {}
    const url = '/paipanmemberservice?appId='+ this.data.appid + '&openId=' + this.data.openId +'&eventCode=1'

    request.get(url,dic).then((res) => {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        const object = res.MEM_INFO
        const tempPRICE_INFO = res.PRICE_INFO

        that.setData({
          userPhone: this.handleReplacePhoneNum(object.tel),
          memberLevel: object.memberLevel,
          memberName: object.memberName,
          userLogo: object.mem_url,
          jndiName: object.jndiName,
          overDate: object.overDate,
          PRICE_INFO: tempPRICE_INFO,
          oneArr: tempPRICE_INFO['1天'],
          twoArr: tempPRICE_INFO['3天'],
          threeArr: tempPRICE_INFO['7天'],
          fourArr: tempPRICE_INFO['31天'],
        })
      }
    })
  },

  /**
   * 处理电话号码
   */
  handleReplacePhoneNum(telphone) {
    if (telphone.length == 0) {
      return ""
    }
    let result = telphone.replace(telphone.substring(3, 8), "****");
    return result;
  },

  handleToRecharge() {
    wx.hideTabBar({
      animation: false,
      success:(res) => {
        this.setData({
          show: true,
        });
      },
      fail: function(err) {
      }
    });
    

  },

  selectPlan(e) {
    console.log(e)
    const { days, price } = e.currentTarget.dataset;
    this.setData({
      selectedPlan: {
        days,
        price
      }
    });
    console.log(this.data.selectedPlan.days)
  },

  closeModal() {
    const that = this
    wx.showTabBar({
      animation: false,
      success:(res) => {
        that.setData({
          show: false,
          selectedPlan: null,
        });
      },
      fail: function(err) {

      }
    });
    
  },

  handlePay() {
    this.getWxgetprecodeservlet()
  },

  stopPropagation() {
    // 阻止事件冒泡
  },

  getWxgetprecodeservlet(){
    console.log(this.data.selectedPlan.price)
    const that = this
    wx.login({
      success: (res) => {
        let dic = {
          appid: that.data.appid,//小程序APPID
          secret: "",//小程序密阴传空就行；
          appkey: "",//商户支付密阴传空就行；
          js_code: res.code,
          grant_type: 'authorization_code',//固定值
          wxUserInfo: 'intuit',//固定值
          total_fee: that.data.selectedPlan.price,//金额
          jndiName: that.data.jndiName,//paiPanItems.jndiName
          mch_id: "",//微信支付商户号，传空就行
          cardID: that.data.memberID //paiPanItems.paiPanItemsCode
        }
        console.log(dic)
        request.get('/wxgetprecodeservlet',dic).then((res) => {
          console.log(res)
          console.log(res.return)
          const returnCode = res.return
          if (returnCode == 1) {
            that.setData({
              nonceStr: res.nonceStr,
              notify_url: res.notify_url,
              openId: res.openId,
              package: res.package,
              prepay_id: res.prepay_id,
              sign: res.sign,
              timeStamp: res.timeStamp,
              total_fee: res.total_fee,
            })
            // console.log(that.data.nonceStr)
            // console.log(that.data.notify_url)
            // console.log(that.data.openId)
            // console.log(that.data.package)
            // console.log(that.data.prepay_id)
            // console.log(that.data.sign)
            // console.log(that.data.timeStamp)
            // console.log(that.data.total_fee)
            that.requestPaymentToWx()
          }
        })
      },
    })
    
  },

  requestPaymentToWx(){
    console.log("requestPaymentToWx");
    const that = this
    wx.requestPayment({
      timeStamp: this.data.timeStamp,
      nonceStr: this.data.nonceStr,
      package: this.data.package,
      signType: 'MD5',
      paySign: this.data.sign,
      success (res) { 
        console.log(res)
        that.getPaipanmemberservice()
        wx.showTabBar({
          animation: false,
          success:(res) => {
            that.setData({
              show: false,
            });
          },
          fail: function(err) {
          }
        })
      },
      fail (res) { }
    })
  },


  
  /**
   * 退出登陆
   */
  handleToLogout() {
    if (this.data.userLogin == false) {
      return;
    }
    const that = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否退出?',
      complete: (res) => {
        if (res.cancel) {
        }
        if (res.confirm) {
          that.postManageLogout();
        }
      }
    })
  },
  
  handleToOrder() {
    if (this.data.memberID == '' || this.data.memberID == '0') {
      nav.navigateTo('/login/login')
    }else {
      nav.navigateTo('/order/order')
    }
  },


  /**
   * 退出系统
   */
  postManageLogout() {
    this.dealUserData();
  },

  /**
   * 处理退出系统的相关信息
   */
  dealUserData() {
    // auth.setToken({});
    local.setLocal("memberID", '');
    // nav.reLaunch("/login/login");
    this.setData({
      userLogo: "",
      companyName: "",
      memberName: '',
      userPhone: "去登陆",
      memberID: '',
      isLogin: false,
    });
  },
  
})